home *** CD-ROM | disk | FTP | other *** search
/ IRIX Patches 1995 June / SGI IRIX Patches 1995 Jun.iso / 5.3_patches / patchSG0000154 / patchSG0000154.idb / usr / share / src / OpenGL / exts / abgr.c.z / abgr.c
Encoding:
C/C++ Source or Header  |  1995-06-12  |  5.1 KB  |  223 lines

  1. /*
  2. ** abgr.c - Demonstrates the use of the extension EXT_abgr.
  3. **
  4. ** The same image data is used for both ABGR and RGBA formats
  5. ** in glDrawPixels and glTexImage2D.  The left side uses ABGR,
  6. ** the right side RGBA.  The top polygon demonstrates use of texture,
  7. ** and the bottom image is drawn with glDrawPixels.
  8. **
  9. ** Note that the textures are defined as 3 component, so the alpha
  10. ** value is not used in applying the DECAL environment.
  11. */
  12.  
  13. #include <string.h>
  14. #include <unistd.h>
  15. #include <stdlib.h>
  16. #include "tk.h"
  17.  
  18. GLenum doubleBuffer, directRender;
  19. GLint windW, windH;
  20. GLubyte ubImage[65536];
  21.  
  22. static void Init(void)
  23. {
  24.     int i, j;
  25.     GLubyte *img;
  26.     GLsizei imgWidth = 128;
  27.  
  28.     glDisable(GL_DITHER);
  29.  
  30.     /* Create image */
  31.     img = ubImage;
  32.     for (j = 0; j < 32 * imgWidth; j++) {
  33.     *img++ = 0xff;
  34.     *img++ = 0x00;
  35.     *img++ = 0x00;
  36.     *img++ = 0xff;
  37.     }
  38.     for (j = 0; j < 32 * imgWidth; j++) {
  39.     *img++ = 0xff;
  40.     *img++ = 0x00;
  41.     *img++ = 0xff;
  42.     *img++ = 0x00;
  43.     }
  44.     for (j = 0; j < 32 * imgWidth; j++) {
  45.     *img++ = 0xff;
  46.     *img++ = 0xff;
  47.     *img++ = 0x00;
  48.     *img++ = 0x00;
  49.     }
  50.     for (j = 0; j < 32 * imgWidth; j++) {
  51.     *img++ = 0x00;
  52.     *img++ = 0xff;
  53.     *img++ = 0x00;
  54.     *img++ = 0xff;
  55.     }
  56. }
  57.  
  58. static void Reshape(int width, int height)
  59. {
  60.  
  61.     windW = (GLint)width;
  62.     windH = (GLint)height;
  63.  
  64.     glViewport(0, 0, (GLint)width, (GLint)height);
  65.     glMatrixMode(GL_PROJECTION);
  66.     glLoadIdentity();
  67.     gluPerspective(60.0, 1.0, 0.1, 1000.0);
  68.     glMatrixMode(GL_MODELVIEW);
  69. }
  70.  
  71. static GLenum Key(int key, GLenum mask)
  72. {
  73.  
  74.     switch (key) {
  75.       case TK_ESCAPE:
  76.     tkQuit();
  77.       default:
  78.     return GL_FALSE;
  79.     }
  80.     return GL_TRUE;
  81. }
  82.  
  83. /* Draw textured polygons */
  84. void texFunc(void)
  85. {
  86.     GLenum err;
  87.  
  88.     glTexImage2D(GL_TEXTURE_2D, 0, 3, 128, 128, 0, GL_ABGR_EXT,
  89.                 GL_UNSIGNED_BYTE, ubImage);
  90.     err = glGetError();
  91.     if (err) printf("err %d\n",err);
  92.  
  93.     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  94.     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  95.     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  96.     err = glGetError();
  97.     if (err) printf("err %d\n",err);
  98.  
  99.     glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
  100.     glEnable(GL_TEXTURE_2D);
  101.  
  102.     glBegin(GL_POLYGON);
  103.         glTexCoord2f(1.0, 1.0); glVertex3f(-0.2, 0.8, -100.0);
  104.         glTexCoord2f(0.0, 1.0); glVertex3f(-0.8, 0.8, -2.0);
  105.         glTexCoord2f(0.0, 0.0); glVertex3f(-0.8, 0.2, -2.0);
  106.         glTexCoord2f(1.0, 0.0); glVertex3f(-0.2, 0.2, -100.0);
  107.     glEnd();
  108.  
  109.     glTexImage2D(GL_TEXTURE_2D, 0, 3, 128, 128, 0, GL_RGBA,
  110.                 GL_UNSIGNED_BYTE, ubImage);
  111.  
  112.     glBegin(GL_POLYGON);
  113.         glTexCoord2f(1.0, 1.0); glVertex3f(0.8, 0.8, -2.0);
  114.         glTexCoord2f(0.0, 1.0); glVertex3f(0.2, 0.8, -100.0);
  115.         glTexCoord2f(0.0, 0.0); glVertex3f(0.2, 0.2, -100.0);
  116.         glTexCoord2f(1.0, 0.0); glVertex3f(0.8, 0.2, -2.0);
  117.     glEnd();
  118.  
  119.     glDisable(GL_TEXTURE_2D);
  120.     err = glGetError();
  121.     if (err) printf("err %d\n",err);
  122. }
  123.  
  124. static void Draw(void)
  125. {
  126.  
  127.     glClearColor(0.0, 0.0, 0.0, 1.0);
  128.     glClear(GL_COLOR_BUFFER_BIT);
  129.  
  130.     glRasterPos3f(-0.8, -0.8, -1.5);
  131.     glDrawPixels(128, 128, GL_ABGR_EXT, GL_UNSIGNED_BYTE, ubImage);
  132.  
  133.     glRasterPos3f(0.2, -0.8, -1.5);
  134.     glDrawPixels(128, 128, GL_RGBA, GL_UNSIGNED_BYTE, ubImage);
  135.  
  136.     texFunc();
  137.  
  138.     if (doubleBuffer) {
  139.     tkSwapBuffers();
  140.     }
  141. }
  142.  
  143. static GLenum Args(int argc, char **argv)
  144. {
  145.     GLint i;
  146.  
  147.     doubleBuffer = GL_FALSE;
  148.     directRender = GL_TRUE;
  149.  
  150.     for (i = 1; i < argc; i++) {
  151.     if (strcmp(argv[i], "-sb") == 0) {
  152.         doubleBuffer = GL_FALSE;
  153.     } else if (strcmp(argv[i], "-db") == 0) {
  154.         doubleBuffer = GL_TRUE;
  155.     } else if (strcmp(argv[i], "-dr") == 0) {
  156.         directRender = GL_TRUE;
  157.     } else if (strcmp(argv[i], "-ir") == 0) {
  158.         directRender = GL_FALSE;
  159.     } else {
  160.         printf("%s (Bad option).\n", argv[i]);
  161.         return GL_FALSE;
  162.     }
  163.     }
  164.     return GL_TRUE;
  165. }
  166.  
  167.  
  168. static GLboolean queryExtension(char *extName)
  169. {
  170.     /*
  171.     ** Search for extName in the extensions string.  Use of strstr()
  172.     ** is not sufficient because extension names can be prefixes of
  173.     ** other extension names.  Could use strtok() but the constant
  174.     ** string returned by glGetString can be in read-only memory.
  175.     */
  176.     char *p = (char *) glGetString(GL_EXTENSIONS);
  177.     char *end = p + strlen(p);
  178.     while (p < end) {
  179.     int n = strcspn(p, " ");
  180.     if ((strlen(extName) == n) && (strncmp(extName, p, n) == 0)) {
  181.         return GL_TRUE;
  182.     }
  183.     p += (n + 1);
  184.     }
  185.     return GL_FALSE;
  186. }
  187.  
  188.  
  189. void main(int argc, char **argv)
  190. {
  191.     GLenum type;
  192.     const GLubyte *s;
  193.  
  194.     if (Args(argc, argv) == GL_FALSE) {
  195.     tkQuit();
  196.     }
  197.  
  198.     tkInitPosition(0, 0, 400, 400);
  199.  
  200.     type = TK_RGB;
  201.     type |= (doubleBuffer) ? TK_DOUBLE : TK_SINGLE;
  202.     type |= (directRender) ? TK_DIRECT : TK_INDIRECT;
  203.     tkInitDisplayMode(type);
  204.  
  205.     if (tkInitWindow("ABGR extension") == GL_FALSE) {
  206.     tkQuit();
  207.     }
  208.  
  209.     /* Make sure blend_logic_op extension is there. */
  210.     if (!queryExtension("GL_EXT_abgr")) {
  211.        printf("couldn't find blend_logic_op extension\n");
  212.        tkQuit();
  213.     }
  214.  
  215.     Init();
  216.  
  217.     tkExposeFunc(Reshape);
  218.     tkReshapeFunc(Reshape);
  219.     tkKeyDownFunc(Key);
  220.     tkDisplayFunc(Draw);
  221.     tkExec();
  222. }
  223.